package com.elinkway.infinitemovies.utils;

import android.content.Context;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.AdDataBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.AdDataParser;
import com.lvideo.http.bean.LVideoDataHull;

/**
 * 请求广告数据
 * 
 * @author wangyemin
 * 
 */
public class RequestAdDataHelper {
	private String TAG = "RequestAdDataHelper";
	private Context mContext;

	public RequestAdDataHelper(Context context) {
		mContext = context;
	}

	public void requestBannerAdData(String pageFrom,
			RequestResultListener<AdDataBean> listener) {
		new RequestAdDataTask(mContext, pageFrom, listener).start();
	}

	private class RequestAdDataTask extends MoviesHttpAsyncTask<AdDataBean> {
		private RequestResultListener<AdDataBean> mListener;
		private String mPageFrom;

		public RequestAdDataTask(Context mContext, String pageFrom,
				RequestResultListener<AdDataBean> listener) {
			super(mContext);
			mListener = listener;
			mPageFrom = pageFrom;
		}

		@Override
		public LVideoDataHull<AdDataBean> doInBackground() {
			return MoviesHttpApi.requestAdData(new AdDataParser(mPageFrom),
					mPageFrom);
		}

		@Override
		public void onPostExecute(int updateId, AdDataBean result) {
			if (mListener != null) {
				mListener.onRequestSuccess(updateId, result);
			}
		}

		/**
		 * 没有网络，回调
		 * */
		@Override
		public void netNull() {
			super.netNull();
			LogUtils.e(TAG, "没有网络");
			if (mListener != null) {
				mListener.onRequestFailed();
			}
		}

		/**
		 * 网络异常和数据错误，回调
		 * */
		@Override
		public void netErr(int updateId, String errMsg) {
			super.netErr(updateId, errMsg);
			LogUtils.e(TAG, "网络异常和数据错误!!! and errMsg is " + errMsg);
			if (mListener != null) {
				mListener.onRequestFailed();
			}
		}

		/**
		 * 数据为空，回调
		 * */
		@Override
		public void dataNull(int updateId, String errMsg) {
			super.dataNull(updateId, errMsg);
			LogUtils.e(TAG, "数据为空!!! and errMsg is " + errMsg);
			if (mListener != null) {
				mListener.onRequestFailed();
			}
		}
	}
}
